package com.rst.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rst.dao.SubjectDAO;
import com.rst.model.Section;
import com.rst.model.Subject;

@Service
public class SubjectService {

	@Autowired
	private SubjectDAO subjectDAO;
	
	public String deleteSubject(Long sectionId) {
		Subject subject = subjectDAO.getSubjectBySubjectId(sectionId);
		subject.setStatus(Section.INACTIVE);
		subjectDAO.saveSubject(subject);
		return "success";
	}

	public void saveSubject(Subject subject) {
		 subjectDAO.saveSubject(subject);		
	}

	public Subject getSubjectBySubjectId(Long subjectId) {
		return subjectDAO.getSubjectBySubjectId(subjectId);		
	}

	public List<Subject> getSubjectListBySectionId(Long sectionId) {
		return subjectDAO.getSubjectListBySectionId(sectionId);		
		}

	public String processSubjectList(List<Subject> subjectList) {		
		String ajaxString = "";
        if(subjectList!= null && subjectList.size()>0){
        	ajaxString = ajaxString + "<div class='panel panel-default'><div class='panel-heading'><h3 class='panel-title'>Subject List</h3></div><div class='panel-body'>"
        			+ "<div class='table-responsive'><table class='table table-striped table-bordered table-hover'><tbody>";
        	for (Subject subject : subjectList) {
        		ajaxString = ajaxString + "<tr><td>"+ subject.getName() +"-" +subject.getCode()+"</td>"; 
        		ajaxString = ajaxString+ "<td>";	
        		ajaxString = ajaxString+ "<a href='<%=request.getContextPath() %>/addSection.htm'><button class='btn btn-primary btn-xs' type='button'>Add</button></a>";
        		ajaxString = ajaxString+ "&nbsp;&nbsp;<a href='<%=request.getContextPath() %>/editSection.htm?id="+subject.getSectionId()+"'><button class='btn btn-primary btn-xs' type='button'>Edit</button></a>";
                ajaxString = ajaxString+ "&nbsp;&nbsp;<a href='<%=request.getContextPath() %>/deleteSection.htm?id="+subject.getSectionId()+"'><button class='btn btn-danger btn-xs' type='button'>Delete</button></a>";
                ajaxString = ajaxString+ "</td></tr>";
			}
        	ajaxString = ajaxString + "</tbody></table></div></div></div>";
        }else{
        	ajaxString = ajaxString + "empty";
        }
		return ajaxString;
	}

	
}
